package com.orhon.smartcampus.modules.systemctl.fetcher;

import com.orhon.smartcampus.modules.systemctl.entity.Apis;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLApis;
import com.orhon.smartcampus.modules.systemctl.mm.ApisMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.modules.systemctl.dao.WidgetsDao;
import com.orhon.smartcampus.modules.systemctl.entity.Menus;
import com.orhon.smartcampus.modules.systemctl.entity.Modules;
import com.orhon.smartcampus.modules.systemctl.entity.Widgets;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLMenus;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLModule;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLWidgets;
import com.orhon.smartcampus.modules.systemctl.mm.MenuMapper;
import com.orhon.smartcampus.modules.systemctl.mm.ModuleMapper;
import com.orhon.smartcampus.modules.systemctl.mm.WidgetsMapper;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

import java.util.List;

@Component
public class WidgetFetcher implements DataFetcher {

	@Autowired
	WidgetsDao widgetsDao;

	@Override
	public Object get(DataFetchingEnvironment env) throws Exception {
		int id = Integer.parseInt(env.getArgument("id").toString());

		String hql = "from Widgets where deleted_at = null and id = "+id;
		Widgets item =  widgetsDao.findId(hql, null);
		if(item!=null) {
			GQLMenus gqlMenus = null;
			if (env.getSelectionSet().contains("menu")){
				Menus menus = item.getSystemctlMenusByMenuId();
				gqlMenus = MenuMapper.MAPPER.toMenus(menus);
			}
			GQLModule gqlModule = null;
			if (env.getSelectionSet().contains("module")){
				Modules modules = item.getSystemctlModulesByModuleId();
				gqlModule = ModuleMapper.MAPPER.mapModule(modules);
			}

			List<GQLApis> GQLApis= null;
			if (env.getSelectionSet().contains("data/operationApiRelations")){
				List<Apis> lists = item.getApis();
				GQLApis = ApisMapper.MAPPER.toApisList(lists);
			}
			GQLWidgets widgets = WidgetsMapper.MAPPER.toWidgets(item);
			widgets.setMenu(gqlMenus);
			widgets.setModule(gqlModule);
			widgets.setApis(GQLApis);

			return widgets;
		}
		return null;

	}


}
